package com.ycrlkj.android.modulescore.main.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import com.lxj.xpopup.impl.LoadingPopupView;
import com.ycrlkj.android.modulescore.main.BR;
import com.ycrlkj.android.modulescore.main.R;
import com.ycrlkj.android.modulescore.main.apiservice.XxStationApiService;
import com.ycrlkj.android.modulescore.main.databinding.ActivitySelectXxStationBinding;
import com.ycrlkj.android.modulescore.main.entity.XxStation;
import com.ycrlkj.android.modulescore.main.entity.Query.XxStationQuery;
import com.ycrlkj.android.modulespublic.common.base.DataBindingRecyclerViewAdapter;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiException;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiExceptionHandler;
import com.ycrlkj.android.modulespublic.common.net.exception.ErrorConsumer;
import com.ycrlkj.android.modulespublic.common.net.transformer.ResponseTransformer;
import com.ycrlkj.android.modulespublic.common.utils.xpopup.XPopupUtil;
import com.ycrlkj.android.modulespublic.common.widget.titlebar.MTitleBar;

import java.io.Serializable;

import javax.inject.Inject;

public class SelectXxStationActivity extends AppCompatActivity {

    @Inject
    XxStationApiService xxStationApiService;

    private ActivitySelectXxStationBinding binding;

    private RecyclerView recyclerView;

    private DataBindingRecyclerViewAdapter<XxStation> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySelectXxStationBinding.inflate(getLayoutInflater());

        setContentView(binding.getRoot());

        initTitleBar();
        initAdapter();
        getAll();
    }

    public void initAdapter() {
        recyclerView = binding.recyclerView;
        recyclerView.setLayoutManager(new LinearLayoutManager(binding.getRoot().getContext()));
        adapter = new DataBindingRecyclerViewAdapter<>(binding.getRoot().getContext(), R.layout.item_select_freight_order, BR.xxStation);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new DataBindingRecyclerViewAdapter.OnItemClickListener<XxStation>() {
            @Override
            public void onItemClickListener(XxStation item, int i) {
                backToXxAppointmentAdd(item);
            }
        });
    }

    public void backToXxAppointmentAdd(XxStation xxStation){
        Intent intent = new Intent();
        intent.putExtra("xxStation", (Serializable) xxStation);
        setResult(2, intent);
        finish();
    }


    @SuppressLint("CheckResult")
    public void getAll() {
        LoadingPopupView loadingPopupView = XPopupUtil.showLoading(this);
        XPopupUtil.hideLoadingDelay(loadingPopupView);

        XxStationQuery xxStationQuery = new XxStationQuery();

        xxStationApiService.getAll(xxStationQuery)
                .compose(ResponseTransformer.obtain())
                .subscribe(xxStations -> {
                            adapter.add(xxStations);
                            XPopupUtil.hideLoading(loadingPopupView);
                        },
                        new ErrorConsumer() {
                            @Override
                            protected void error(ApiException e) {
                                XPopupUtil.hideLoading(loadingPopupView);
                                ApiExceptionHandler.doWith(SelectXxStationActivity.this, e);
                            }
                        });

    }

    public void initTitleBar() {
        MTitleBar titleBar = binding.mTitleBar;
        titleBar.setBtnEvents(new MTitleBar.BtnEvents() {
            @Override
            public void setBtnLeftOnClickListener() {
                finish();
            }

            @Override
            public void setBtnRightOnClickListener() {
            }
        });
    }

}